package com.example.mypra1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;

import com.example.mypra1.adapter.MyListAdapter;
import com.example.mypra1.adapter.entity.Comment;
import com.example.mypra1.fragments.BottomDialogFragment;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 我的发布
 */
public class GuanzhuActivity extends AppCompatActivity {
    private ListView lv;
    private List<Comment>comments;
    private String profile,username;
    private int id;
    private Handler handler=new Handler(){

        @Override
        public void handleMessage(@NonNull Message msg) {
            Map<String,Object> map = (Map<String, Object>) msg.obj;
            comments = (List<Comment>) map.get("comments");
            lv = findViewById(R.id.lv);
            //列表适配器同MainFragment3
            MyListAdapter adapter = new MyListAdapter(GuanzhuActivity.this,R.layout.project_item,comments){
                public void showReply(int position){
                    Bundle bundle = new Bundle();
                    Comment comment = comments.get(position);
                    bundle.putInt("id",comment.getId());
                    bundle.putInt("rep_count",comment.getRepcount());
                    bundle.putString("profile",profile);
                    bundle.putString("pubname",username);
                    bundle.putString("pubtime",comment.getPubtime());
                    bundle.putString("content",comment.getContent());
                    bundle.putString("imgs",comment.getImgs());
                    BottomDialogFragment bottomDialogFragment = new BottomDialogFragment();
                    bottomDialogFragment.setArguments(bundle);
                    bottomDialogFragment.show(getSupportFragmentManager(),"tag");
                }
            };
            lv.setAdapter(adapter);

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guanzhu);
        SharedPreferences pref = getSharedPreferences("data", Context.MODE_PRIVATE);
        //从SharedPreference存储中获取当前用户信息
        profile = pref.getString("profile","");
        username = pref.getString("username","");
        id = pref.getInt("id",1);
        getData();
    }
    //get请求获取当前用户发布过的帖子
    private void getData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://8.142.19.67:3000/myComments?pubid="+id)
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException,RuntimeException {
                            Log.d("resp",response.toString());
                            if (response.isSuccessful()) {
                                String res = response.body().string();
                                Log.d("res",res);
                                try {
                                    JSONObject jsonObject = new JSONObject(res);
                                    JSONArray jsonArray = jsonObject.getJSONArray("data");
                                    Log.d("array",jsonArray.toString());
                                    Map<String,Object> map = new HashMap<>();
                                    List<Comment> comments1 = new ArrayList<>();
                                    for(int i=0;i<jsonArray.length();i++){
                                        JSONObject object = jsonArray.getJSONObject(i);
                                        comments1.add(new Comment(object.getInt("id"),object.getInt("pubid"),
                                                username,profile,object.getString("content"),
                                                object.getString("imgs"),object.getString("pubtime"),object.getInt("favor"),
                                                object.getInt("repcount")));
                                    }
                                    map.put("comments",comments1);
                                    Message message = new Message();
                                    message.obj = map;
                                    handler.sendMessage(message);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }else{
                                Log.d("resp",response.toString());
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}